package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/13.<br><br>
 *
 * 4的幂
 */
public class Topic342 {
    public static void main(String[] args) {
       Topic342 instance = new Topic342();

        System.out.println(instance.isPowerOfFour(16));
        System.out.println(instance.isPowerOfFour(5));
        System.out.println(instance.isPowerOfFour(1));
    }

    public boolean isPowerOfFour(int n) {
        if(n == 1) {
            return true;
        }

        if(n < 1 || (n & 1) != 0) {
            return false;
        }

        for(int i = 2; i < 32; i = i + 2) {
            if(1 << i == n) {
                return true;
            }
        }

        return false;
    }

    public boolean isPowerOfFour3(int n) {
        if(n == 1) {
            return true;
        }

        if(n < 1 || (n & 1) != 0) {
            return false;
        }

        while (n != 1) {
            if(n % 4 != 0) {
                return false;
            }

            n = n / 4;
        }

        return true;
    }

    public boolean isPowerOfFour2(int n) {
        return (n > 0) && ((n & n - 1) == 0) && ((n & 0xaaaaaaaa) == 0);
    }
}
